﻿using Microsoft.AspNetCore.Mvc;
using Zhaoxi.NET6.Interfaces;

namespace NET6Demo.WebApi.Controllers
{
    /// <summary>
    /// 参数来源，参数多种修饰特性的核心价值
    /// 1  FromServices 
    /// 2  FromBody 
    /// 3  FromForm 
    /// 4  FromHeader 
    /// 5  FromQuery 
    /// 6  FromRoute
    /// </summary>
    [ApiController]
    [ApiVersion("1.0")]
    [Route("[controller]/v{version:apiVersion}")]
    public class ParameterFromController : ControllerBase
    {
        private readonly ILogger<ParameterFromController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        public ParameterFromController(ILogger<ParameterFromController> logger)
        {
            _logger = logger;
        }

        #region FromServices 
        /// <summary>
        /// FromServices修饰
        /// 1.表示来自于IOC容器创建
        /// 2.必然需要IOC容器先注册
        /// 
        /// 如果没有标记 [FromServices]，默认会认定这个参数是要通过调用方传递
        /// 
        /// 控制器不是已经有了构造函数注入吗？为什么方法中还提供[FromServices]特性在方法的参数中来获取服务实例呢？ 为了提高性能~
        /// 
        /// </summary>
        /// <param name="ITestServiceB"></param>
        /// <returns></returns>
        [HttpGet()]
        [Route("FromServicesMethod")]
        public string FromServicesMethod([FromServices] ITestServiceB ITestServiceB)
        {
            return ITestServiceB.ShowB();
        }

        #endregion

        #region FromBody修饰 

        /// <summary>
        /// FromBody修饰-Get请求---不能访问
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpGet()]
        [Route("FromBodyMethodGet")]
        public User FromBodyMethodGet([FromBody] User user)
        {
            return user;
        }

        /// <summary>
        /// FromBody修饰-Post请求---可以访问
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost()]
        [Route("FromBodyMethodPost")]
        public User FromBodyMethodPost([FromBody] User user)
        {
            return user;
        }

        /// <summary>
        /// FromBody修饰-Put请求---可以访问
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPut()]
        [Route("FromBodyMethodPut")]
        public User FromBodyMethodPut([FromBody] User user)
        {
            return user;
        }

        /// <summary>
        /// FromBody修饰-Delete请求---可以访问
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpDelete()]
        [Route("FromBodyMethodDelete")]
        public User FromBodyMethodDelete([FromBody] User user)
        {
            return user;
        }

        #endregion

        #region FromForm修饰

        /// <summary>
        /// FromForm修饰-Get请求---不能访问
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpGet()]
        [Route("FromFormMethodGet")]
        public User FromFormMethodGet([FromForm] User user)
        {
            return user;
        }

        /// <summary>
        /// FromForm修饰-Post请求---可以访问
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost()]
        [Route("FromFormMethodPost")]
        public User FromFormMethodPost([FromForm] User user)
        {
            return user;
        }

        /// <summary>
        /// FromForm修饰-Put请求---可以访问
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPut()]
        [Route("FromFormMethodPut")]
        public User FromFormMethodPut([FromForm] User user)
        {
            return user;
        }

        /// <summary>
        /// FromForm修饰-Delete请求---可以访问
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpDelete()]
        [Route("FromFormMethodDelete")]
        public User FromFormMethodDelete([FromForm] User user)
        {
            return user;
        }

        #endregion

        #region FromHeader


        /// <summary>
        /// FromHeader修饰-Get请求---可以访问
        /// </summary>
        /// <param name="header"></param>
        /// <returns></returns>
        [HttpGet()]
        [Route("FromHeaderMethodGet")]
        public string FromHeaderMethodGet([FromHeader] string header)
        {
            return header;
        }

        /// <summary>
        /// FromHeader修饰-Post请求---可以访问
        /// </summary>
        /// <param name="header"></param>
        /// <returns></returns>
        [HttpPost()]
        [Route("FromHeaderMethodPost")]
        public string FromHeaderMethodPost([FromHeader] string header)
        {
            return header;
        }

        /// <summary>
        /// FromHeader修饰-Put请求---可以访问
        /// </summary>
        /// <param name="header"></param>
        /// <returns></returns>
        [HttpPut()]
        [Route("FromHeaderMethodPut")]
        public string FromHeaderMethodPut([FromHeader] string header)
        {
            return header;
        }

        /// <summary>
        /// FromHeader修饰-Delete请求---可以访问
        /// </summary>
        /// <param name="header"></param>
        /// <returns></returns>
        [HttpDelete()]
        [Route("FromHeaderMethodDelete")]
        public string FromHeaderMethodDelete([FromHeader] string header)
        {
            return header;
        }

        #endregion

        #region FromQuery修饰

        /// <summary>
        /// FromQuery修饰-Get请求
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet()]
        [Route("FromQueryMethodGet")]
        public string FromQueryMethodGet([FromQuery] string query)
        {
            return query;
        }

        /// <summary>
        /// FromQuery修饰-Post请求
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost()]
        [Route("FromQueryMethodPost")]
        public string FromQueryMethodPost([FromQuery] string query)
        {
            return query;
        }

        /// <summary>
        /// FromQuery修饰-Put请求
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPut()]
        [Route("FromQueryMethodPut")]
        public string FromQueryMethodPut([FromQuery] string query)
        {
            return query;
        }

        /// <summary>
        /// FromQuery修饰-Delete请求
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpDelete()]
        [Route("FromQueryMethodDelete")]
        public string FromQueryMethodDelete([FromQuery] string query)
        {
            return query;
        }

        #endregion

        #region FromRoute修饰

        /// <summary>
        ///  FromRoute修饰--Get请求--
        /// </summary>
        /// <param name="route"></param>
        /// <returns></returns>
        [HttpGet()]
        [Route("FromRouteMethodGet/{route}")]
        public string FromRouteMethodGet([FromRoute] string route)
        {
            return route;
        }

        /// <summary>
        /// FromRoute修饰--Post请求
        /// </summary>
        /// <param name="route"></param>
        /// <returns></returns>
        [HttpPost()]
        [Route("FromRouteMethodPost/{route}")]
        public string FromRouteMethodPost([FromRoute] string route)
        {
            return route;
        }

        /// <summary>
        /// FromRoute修饰--Put请求
        /// </summary>
        /// <param name="route"></param>
        /// <returns></returns>
        [HttpPut()]
        [Route("FromRouteMethodPut/{route}")]
        public string FromRouteMethodPut([FromRoute] string route)
        {
            return route;
        }

        /// <summary>
        /// FromRoute修饰--Delete请求
        /// </summary>
        /// <param name="route"></param>
        /// <returns></returns>
        [HttpDelete()]
        [Route("FromRouteMethodDelete/{route}")]
        public string FromRouteMethodDelete([FromRoute] string route)
        {
            return route;
        }
        #endregion 
    }
}
